package com.leo.erp.web.inoutitem;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.leo.erp.dto.InOutItemExecution;
import com.leo.erp.entity.InOutItem;
import com.leo.erp.enums.InOutItemStateEnum;
import com.leo.erp.service.InOutItemService;
import com.leo.erp.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/inOutItem")
public class InOutItemManagementController {
	@Autowired
	private InOutItemService inOutItemService;
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		String name = HttpServletRequestUtil.getString(request, "name");
		String type = HttpServletRequestUtil.getString(request, "type");
		String remark = HttpServletRequestUtil.getString(request, "remark");
		
		InOutItemExecution ie = inOutItemService.getList(name, type, remark, currentPage, pageSize);
		if(ie.getState() == InOutItemStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("pages",ie.getPages());//总页数
			modelMap.put("total", ie.getTotal());//总数
			
			Map<String, Object> map = new HashMap<>();
			map.put("rows", ie.getInOutItemPageList());
			map.put("total", ie.getTotal());
			//modelMap.put("userList", ue.getUserPageList());
			modelMap.put("inOutItemList", map);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/checkIsNameExist")
	@ResponseBody
	public Map<String, Object> checkIsNameExist(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int inOutItemId = HttpServletRequestUtil.getInt(request, "id");
		String name = HttpServletRequestUtil.getString(request, "name");
		
		//调用方法
		InOutItemExecution ie = inOutItemService.checkIsNameExist(inOutItemId, name);
		if(ie.getState() == InOutItemStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);//存在
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		int inOutItemId = HttpServletRequestUtil.getInt(request, "id");
		InOutItemExecution ie = inOutItemService.updateInOutItem(beanJson, inOutItemId, request);
		if(ie.getState() == InOutItemStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		InOutItemExecution ie = inOutItemService.insertInOutItem(beanJson, request);
		if(ie.getState() == InOutItemStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	/**
	 * 批量删除收支项目信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/batchDeleteInOutItemByIds")
	@ResponseBody
	public Map<String, Object> batchDeleteInOutItemByIds(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String ids = HttpServletRequestUtil.getString(request, "ids");
		String deleteType = HttpServletRequestUtil.getString(request, "deleteType");//1:正常删除,2:强制删除
		InOutItemExecution ie = new InOutItemExecution();
		if("1".equals(deleteType)) {
			//正常删除
			ie = inOutItemService.batchDeleteInOutItemByIdsNormal(ids);
			if(ie.getState() == InOutItemStateEnum.DELETE.getState()) {
				modelMap.put("success", false);
				modelMap.put("code", 2);
				modelMap.put("msg", "希望强制删除吗?");
			}else if(ie.getState() == InOutItemStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除收支项目信息失败");
			}
		}else if("2".equals(deleteType)) {
			//强制删除
			int effectedNum = inOutItemService.batchDeleteInOutItemByIds(ids);
			if(effectedNum > 0) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除收支项目信息失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "删除操作被拒绝，请联系管理员");
		}	
		
		return modelMap;
	}
	
	/**
     * 查找收支项目信息-下拉框
     * @param request
     * @return
     */
	@RequestMapping(value = "/findBySelect")
	@ResponseBody
	public String findBySelect(HttpServletRequest request) {
		String res = null;
		//获取前台传过来的值
		String type = HttpServletRequestUtil.getString(request, "type");
		//存放数据json数组
        JSONArray dataArray = new JSONArray();
        InOutItemExecution ie = inOutItemService.findBySelect(type);
        if(ie.getState() == InOutItemStateEnum.SUCCESS.getState()) {
        	for(InOutItem inOutItem : ie.getInOutItemList()) {
        		JSONObject item = new JSONObject();
                item.put("Id", inOutItem.getInOutItemId());
                //收支项目名称
                item.put("InOutItemName", inOutItem.getName());
                dataArray.add(item);
        	}
        }
		res = dataArray.toJSONString();
		return res;
	}
}
